package com.ceiling.service;

import com.ceiling.bean.system.*;
import com.ceiling.bean.system.bo.AdminCreateBo;
import com.ceiling.bean.system.bo.AdminReq;
import com.ceiling.bean.system.bo.LogAndRoleReq;
import com.ceiling.bean.system.vo.*;
import com.ceiling.dao.SystemMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SystemServiceImpl implements SystemService{

    @Autowired
    SystemMapper systemMapper;

    @Override
    public BaseData<Admin> selectAdminList(AdminReq adminReq) {
        //分页
        PageHelper.startPage(adminReq.getPage(),adminReq.getLimit());
        //排序
        AdminExample adminExample = new AdminExample();
        adminExample.setOrderByClause(adminReq.getSort() + " " +adminReq.getOrder());
        AdminExample.Criteria criteria = adminExample.createCriteria();
        String username = adminReq.getUsername();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        List<Admin> admins = systemMapper.selectByAdminExample(adminExample);

        PageInfo<Admin> adminPageInfo = new PageInfo<>(admins);
        long total = adminPageInfo.getTotal();

        return BaseData.ok(admins,total);
    }

    @Override
    public int createAdmin(Admin admin) {
        int i = systemMapper.insertSelective(admin);
        if(i == 1){
            return 200;
        }
        return 404;
    }

    @Override
    public int updateAdmin(Admin admin) {
        int result = systemMapper.updateByPrimaryKeySelective(admin);
        if(result == 1){
            return 200;
        }
        return 404;
    }

    @Override
    public int deleteAdmin(Admin admin) {
        admin.setDeleted(1);
        int i = systemMapper.updateByPrimaryKeySelective(admin);
        if(i == 1){
            return 200;
        }
        return 404;
    }

    @Override
    public AdminLogVo selectLog(LogAndRoleReq logReq) {
        PageHelper.startPage(logReq.getPage(),logReq.getLimit());
        LogExample logExample = new LogExample();
        logExample.setOrderByClause(logReq.getSort() + " " +logReq.getOrder());
        LogExample.Criteria criteria = logExample.createCriteria();
        String name = logReq.getName();
        if(name != null && !"".equals(name)){
            criteria.andAdminLike("%" + name + "%");
        }
        List<Log> logs = systemMapper.selectByLogExample(logExample);
        long total = new PageInfo<>(logs).getTotal();
        AdminLogVo adminLogVo = new AdminLogVo();
        adminLogVo.setTotal(total);
        adminLogVo.setItems(logs);
        return adminLogVo;
    }

    @Override
    public AdminRoleVo selectRoleList(LogAndRoleReq roleReq) {
        PageHelper.startPage(roleReq.getPage(),roleReq.getLimit());
        RoleExample roleExample = new RoleExample();
        roleExample.setOrderByClause(roleReq.getSort() + " " + roleReq.getOrder());
        RoleExample.Criteria criteria = roleExample.createCriteria();
        String name = roleReq.getName();
        if(name != null && !"".equals(name)){
            criteria.andNameLike("%" + name + "%");
        }
        List<Role> roles = systemMapper.selectByRoleExample(roleExample);
        long total = new PageInfo<>(roles).getTotal();
        AdminRoleVo adminRoleVo = new AdminRoleVo();
        adminRoleVo.setTotal(total);
        adminRoleVo.setItems(roles);
        return adminRoleVo;
    }

    @Override
    public RoleCreateVo createRole(Role role) {
        systemMapper.insertRoleSelective(role);
        Integer id = role.getId();
        Role r = systemMapper.selectByRolePrimaryKey(id);
        RoleCreateVo roleCreateVo = new RoleCreateVo();
        roleCreateVo.setId(r.getId());
        roleCreateVo.setName(r.getName());
        roleCreateVo.setDesc(r.getDesc());
        roleCreateVo.setAddTime(r.getAddTime());
        roleCreateVo.setUpdateTime(r.getUpdateTime());
        return roleCreateVo;
    }

    @Override
    public int updateRole(Role role) {

        int i = systemMapper.updateRoleByPrimaryKeySelective(role);
        if(i == 1){
            return 200;
        }
        return 404;
    }

    @Override
    public List<AdminListOptionsVo> queryRole() {
        List<AdminListOptionsVo> options = systemMapper.queryRole();
        return options;

    }

    @Override
    public Admin selectAdminById(Integer id) {
        Admin admin = systemMapper.queryAdminById(id);
        return admin;

    }

//    @Override
//    public int deleteRole(Role role) {
//        role.setDeleted();
//
//    }


}
